<template>
  <Teleport to="body">
    <Transition name="modal">
      <div v-if="show" class="project-modal" @click.self="handleClose">
        <div class="modal-container">
          <button class="close-btn" @click="handleClose">
            <i class="ri-close-line"></i>
          </button>

          <div class="modal-content" v-if="project">
            <!-- 项目封面 -->
            <div class="project-hero">
              <div class="hero-image">
                <img :src="project.cover" :alt="project.name" />
                <div class="hero-overlay"></div>
                <div class="hero-info">
                  <h2 class="hero-title">{{ project.name }}</h2>
                  <p class="hero-desc">{{ project.shortDesc }}</p>
                  <div class="hero-tags">
                    <span v-for="tag in project.tags" :key="tag" class="tag">{{ tag }}</span>
                  </div>
                </div>
              </div>
            </div>

            <!-- 项目操作按钮 -->
            <div class="project-actions">
              <a
                v-if="project.github"
                :href="project.github"
                target="_blank"
                rel="noopener noreferrer"
                class="action-btn github"
              >
                <i class="ri-github-fill"></i>
                <span>GitHub</span>
              </a>
              <a
                v-if="project.gitee"
                :href="project.gitee"
                target="_blank"
                rel="noopener noreferrer"
                class="action-btn gitee"
              >
                <svg viewBox="0 0 1092 1024" width="20" height="20" fill="currentColor">
                  <path
                    d="M514.13 1024c-282.77 0-512-229.23-512-512S231.37 0 514.13 0 1026.13 229.23 1026.13 512 796.9 1024 514.13 1024zm259.16-568.89l-290.76.01c-13.97 0-25.29 11.32-25.29 25.27l-.03 63.22c0 13.97 11.31 25.29 25.27 25.29h177.02c13.97 0 25.29 11.31 25.29 25.27v12.64a75.85 75.85 0 0 1-75.85 75.85H368.74a25.29 25.29 0 0 1-25.29-25.27V417.19a75.85 75.85 0 0 1 75.85-75.85l353.92-.02c13.97 0 25.27-11.31 25.29-25.27l.07-63.19c0-13.97-11.31-25.29-25.27-25.3l-353.99.01c-104.72-.01-189.62 84.89-189.62 189.61v353.96c0 13.97 11.32 25.29 25.29 25.29h372.93c94.27 0 170.67-76.4 170.67-170.67V480.4c0-13.95-11.32-25.27-25.29-25.27z"
                  />
                </svg>
                <span>Gitee</span>
              </a>
              <a
                v-if="project.demo"
                :href="project.demo"
                target="_blank"
                rel="noopener noreferrer"
                class="action-btn demo"
              >
                <i class="ri-external-link-line"></i>
                <span>在线演示</span>
              </a>
            </div>

            <!-- 项目详情 -->
            <div class="project-details">
              <div class="detail-section">
                <h3 class="section-title">
                  <i class="ri-stack-line"></i>
                  <span>技术栈</span>
                </h3>
                <div class="tech-stack">
                  <span v-for="tech in project.technologies" :key="tech" class="tech-item">
                    {{ tech }}
                  </span>
                </div>
              </div>

              <div class="detail-section">
                <h3 class="section-title">
                  <i class="ri-file-text-line"></i>
                  <span>项目介绍</span>
                </h3>
                <p class="section-content">{{ project.description }}</p>
              </div>

              <div class="detail-section" v-if="project.features && project.features.length">
                <h3 class="section-title">
                  <i class="ri-star-line"></i>
                  <span>核心功能</span>
                </h3>
                <ul class="feature-list">
                  <li
                    v-for="(feature, index) in project.features"
                    :key="index"
                    class="feature-item"
                  >
                    <i class="ri-checkbox-circle-fill"></i>
                    <span>{{ feature }}</span>
                  </li>
                </ul>
              </div>
            </div>
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup>
import { watch } from 'vue'

const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
  project: {
    type: Object,
    default: null,
  },
})

const emit = defineEmits(['close'])

const handleClose = () => {
  emit('close')
}

// 监听弹窗显示状态，控制 body 滚动
watch(
  () => props.show,
  (newVal) => {
    if (newVal) {
      document.body.style.overflow = 'hidden'
    } else {
      document.body.style.overflow = ''
    }
  },
  { immediate: true },
)
</script>

<style lang="scss" scoped>
.project-modal {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(8px);
  padding: 20px;

  .modal-container {
    position: relative;
    width: 100%;
    max-width: 900px;
    max-height: 90vh;
    background: var(--bg-base);
    border-radius: 20px;
    overflow: hidden;
    box-shadow: var(--shadow-dark);
    display: flex;
    flex-direction: column;
  }

  .close-btn {
    position: absolute;
    top: 20px;
    right: 20px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.9);
    border: none;
    color: var(--text-primary);
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 10;
    transition: var(--transition-base);

    &:hover {
      background: #fff;
      transform: rotate(90deg);
      color: var(--color-primary);
    }
  }

  .modal-content {
    overflow-y: auto;
    flex: 1;
  }

  .project-hero {
    position: relative;
    height: 400px;
    overflow: hidden;

    .hero-image {
      width: 100%;
      height: 100%;
      position: relative;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

      .hero-overlay {
        position: absolute;
        inset: 0;
        background: linear-gradient(
          to bottom,
          transparent 0%,
          rgba(0, 0, 0, 0.5) 60%,
          rgba(0, 0, 0, 0.8) 100%
        );
      }

      .hero-info {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 40px;
        color: #fff;

        .hero-title {
          font-size: 36px;
          font-weight: 700;
          margin-bottom: 12px;
          text-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
        }

        .hero-desc {
          font-size: 18px;
          margin-bottom: 20px;
          opacity: 0.95;
          text-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
        }

        .hero-tags {
          display: flex;
          flex-wrap: wrap;
          gap: 10px;

          .tag {
            padding: 6px 14px;
            border-radius: 16px;
            font-size: 13px;
            font-weight: 500;
            background: rgba(255, 255, 255, 0.25);
            backdrop-filter: blur(8px);
          }
        }
      }
    }
  }

  .project-actions {
    display: flex;
    gap: 16px;
    padding: 24px 40px;
    border-bottom: 1px solid var(--border-lighter);

    .action-btn {
      flex: 1;
      height: 48px;
      border-radius: 12px;
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 8px;
      font-size: 16px;
      font-weight: 600;
      text-decoration: none;
      transition: var(--transition-base);
      color: #fff;

      i,
      svg {
        font-size: 20px;
      }

      &.github {
        background: #24292e;

        &:hover {
          background: #1a1e22;
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(36, 41, 46, 0.4);
        }
      }

      &.gitee {
        background: #c71d23;

        &:hover {
          background: #a0181d;
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(199, 29, 35, 0.4);
        }
      }

      &.demo {
        background: var(--color-primary);

        &:hover {
          background: var(--primary-color-dark);
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(81, 138, 255, 0.4);
        }
      }
    }
  }

  .project-details {
    padding: 40px;

    .detail-section {
      margin-bottom: 40px;

      &:last-child {
        margin-bottom: 0;
      }

      .section-title {
        display: flex;
        align-items: center;
        gap: 10px;
        font-size: 22px;
        font-weight: 700;
        color: var(--text-primary);
        margin-bottom: 20px;

        i {
          color: var(--color-primary);
          font-size: 24px;
        }
      }

      .tech-stack {
        display: flex;
        flex-wrap: wrap;
        gap: 12px;

        .tech-item {
          padding: 10px 20px;
          border-radius: 12px;
          background: var(--bg-light);
          color: var(--text-primary);
          font-size: 14px;
          font-weight: 500;
          transition: var(--transition-base);

          &:hover {
            background: var(--color-primary);
            color: #fff;
            transform: translateY(-2px);
          }
        }
      }

      .section-content {
        font-size: 16px;
        line-height: 1.8;
        color: var(--text-regular);
      }

      .feature-list {
        list-style: none;
        padding: 0;
        display: flex;
        flex-direction: column;
        gap: 16px;

        .feature-item {
          display: flex;
          align-items: center;
          gap: 12px;
          font-size: 16px;
          line-height: 1.7;
          color: var(--text-regular);

          i {
            color: var(--color-success);
            font-size: 20px;
            flex-shrink: 0;
            line-height: 1;
          }

          span {
            flex: 1;
            line-height: 1.7;
          }
        }
      }
    }
  }
}

// 过渡动画
.modal-enter-active,
.modal-leave-active {
  transition: opacity 0.3s ease;
}

.modal-enter-active .modal-container,
.modal-leave-active .modal-container {
  transition:
    transform 0.3s ease,
    opacity 0.3s ease;
}

.modal-enter-from,
.modal-leave-to {
  opacity: 0;

  .modal-container {
    transform: scale(0.9) translateY(20px);
    opacity: 0;
  }
}

@media (max-width: 768px) {
  .project-modal {
    padding: 10px;

    .modal-container {
      max-height: 95vh;
      border-radius: 16px;
    }

    .close-btn {
      top: 12px;
      right: 12px;
      width: 36px;
      height: 36px;
      font-size: 20px;
    }

    .project-hero {
      height: 250px;

      .hero-info {
        padding: 24px 20px;

        .hero-title {
          font-size: 24px;
        }

        .hero-desc {
          font-size: 14px;
        }
      }
    }

    .project-actions {
      padding: 20px;
      flex-direction: column;

      .action-btn {
        width: 100%;
      }
    }

    .project-details {
      padding: 24px 20px;

      .detail-section {
        margin-bottom: 32px;

        .section-title {
          font-size: 18px;
        }
      }
    }
  }
}
</style>
